import http from '../request';
import appStore from "@/store";
import {ElMessage} from "element-plus";
import {gsvi_path_pre} from "@/assets/ts/constant";
import {storeToRefs} from "pinia";

const {interviewNewStore} = storeToRefs(appStore.useInterviewJsonStore)

// const model_url_base = `http://localhost:9874` //本地模型

const tts_hoyo_body = {
    dl_url: "http://127.0.0.1:9874",
    version: "v4",
    model_name: "原神-芙宁娜",//
    prompt_text_lang: "中文",
    emotion: "默认",
    text: "欢迎使用智聘通，接下来将进行模拟面试演练！",//
    text_lang: "中文",
    top_k: 10,
    top_p: 1,
    temperature: 1,
    text_split_method: "按标点符号切",
    batch_size: 10,
    batch_threshold: 0.75,
    split_bucket: true,
    speed_facter: 1,//
    fragment_interval: 0.3,
    media_type: "wav",
    parallel_infer: true,
    repetition_penalty: 1.35,
    seed: -1,//
    sample_steps: 16,
    if_sr: false
}

const tts_universal_header = {
    Authorization: "Bearer sk-lCTA1Yj5vhH6Q7hq6cC7E0C27eE447C6Bb4b804aB3AdFa89",
    'Content-Type': "application/json"
}

const tts_universal_body = {
    text: "欢迎使用智聘通，接下来将进行模拟面试演练",//
    voice_name: "zh-CN-YunxiNeural",//
    format: "audio-16khz-128kbitrate-mono-mp3",
    speed: "+0%",//
    pitch: "+0Hz"
}

export const getModels: any = () => {
    return http.request({
        url: `${gsvi_path_pre}/models`,
        method: 'post',
        data: {
            "version": "v4"
        }
    });


};

export const getUniversalSpeakers: any = () => {
    return http.request({
        url: `https://tts88.top/cognitiveservices/voices/list`,
        method: 'get',
        headers: {
            'Authorization': 'Bearer sk-lCTA1Yj5vhH6Q7hq6cC7E0C27eE447C6Bb4b804aB3AdFa89'
        }
    });
};

function getSigned(value: number){
    if(value > 0) return `+${value}`
    if(value < 0) return `${value}`
    else return 0
}

function removeMarkdownAndHtml(text: string) {
    // 移除代码块 (```)
    let noCodeBlocks = text.replace(/```[\s\S]*?```/g, '');

    // 移除行内代码 (`code`)
    noCodeBlocks = noCodeBlocks.replace(/`.*?`/g, '');

    // 移除公式块 ($$)
    let noFormulaBlocks = noCodeBlocks.replace(/\$\$[\s\S]*?\$\$/g, '');

    // 移除行内公式 ($formula$)
    noFormulaBlocks = noFormulaBlocks.replace(/\$.*?\$/g, '');

    // 移除标题 (#, ##, ### 等)
    noFormulaBlocks = noFormulaBlocks.replace(/^#+\s+/gm, '');

    // 移除加粗 (**text** 或 __text__)
    noFormulaBlocks = noFormulaBlocks.replace(/(\*\*|__)(.*?)\1/g, '$2');

    // 移除斜体 (*text* 或 _text_)
    noFormulaBlocks = noFormulaBlocks.replace(/([*_])(.*?)\1/g, '$2');

    // 移除删除线 (~~text~~)
    noFormulaBlocks = noFormulaBlocks.replace(/~~(.*?)~~/g, '$1');

    // 移除链接和图片的 Markdown 格式 (![alt](url) 或 [text](url))
    noFormulaBlocks = noFormulaBlocks.replace(/!?\[([^\]]*)]\([^)]*\)/g, '$1');

    // 移除列表标记 (-, *, +, 1., etc.)
    noFormulaBlocks = noFormulaBlocks.replace(/^[\-*+]\s+|^\d+\.\s+/gm, '');

    // 移除所有的 HTML 标签及其中的内容
    noFormulaBlocks = noFormulaBlocks.replace(/<[^>]+>/g, '').replace(/<\/[^>]+>/g, '');

    return noFormulaBlocks.trim();
}


export const getAudio: any = (text?: string,  seed: number = -1) => {
    if(!text){ElMessage({type:"error", message:"请输入内容"}); return ;}
    text = removeMarkdownAndHtml(text);
    console.log(interviewNewStore.value.Settings.model_choice)
    if (interviewNewStore.value.Settings.model_choice === 'universal') {
        let data = {...tts_universal_body};
        data.text = text;
        data.voice_name = interviewNewStore.value.Settings.universal_speaker_name
        data.speed = `${getSigned((interviewNewStore.value.Settings.voice_speed - 1) * 100)}%`
        return http.request({
            url: `https://tts88.top/cognitiveservices/v2`,
            method: 'post',
            headers: {...tts_universal_header},
            data: data,
            responseType: 'arraybuffer'
        })
    }
    else {
        let data = {...tts_hoyo_body}
        data.text = text
        data.model_name = interviewNewStore.value.Settings.hoyo_model_name
        data.speed_facter = interviewNewStore.value.Settings.voice_speed
        data.seed = seed
        return http.request({
            url: `${gsvi_path_pre}/infer_single`,
            method: 'post',
            data: data
        })
    }
}